package edu.berkeley.drivingbuddy;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;

public class ResultsListAdapter extends BaseExpandableListAdapter {
	Context mContext;
	
	List<ResultGroup> mResultGroups;

	public ResultsListAdapter(Context context) {
		mContext = context;
		mResultGroups = DrivingBuddy.getDatastore().getResultGroups();

	}

	public Object getChild(int groupPosition, int childPosition) {
		return mResultGroups.get(groupPosition).getResults().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildType(int groupPosition, int childPosition) {
		return 0;
	}

	public int getChildTypeCount() {
		return 1;
	}

	public int getChildrenCount(int groupPosition) {
		//android.util.Log.d("ResultsListAdapter", "children count: " + mFakeData.get(groupPosition).getResults().size());
		return mResultGroups.get(groupPosition).getResults().size();
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = LayoutInflater.from(mContext).inflate(R.layout.results_list_item, parent, false);

		int attemptIndex = mResultGroups.get(groupPosition).getResults().get(childPosition).getAttemptIndex();
		int score = mResultGroups.get(groupPosition).getResults().get(childPosition).getScore();
		((TextView) convertView.findViewById(R.id.name)).setText("•  Attempt " + attemptIndex);
		((TextView) convertView.findViewById(R.id.score)).setText("" + score);

		return convertView;

	}
	
	public Object getGroup(int groupPosition) {
		return mResultGroups.get(groupPosition);
	}

	public int getGroupType(int groupPosition) {
		return 0;
	}
	
	public int getGroupTypeCount() {
		return 1;
	}

	public int getGroupCount() {
		//android.util.Log.d("ResultsListAdapter", "group count: " + mFakeData.size());
		return mResultGroups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
			ViewGroup parent) {
		if (convertView == null)
			convertView = LayoutInflater.from(mContext).inflate(R.layout.results_list_header, parent, false);

		String name = mResultGroups.get(groupPosition).getName();
		List<ResultObj> results = mResultGroups.get(groupPosition).getResults();
		int score = results.get(results.size()-1).getScore();
		long time = results.get(results.size()-1).getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yy HH:mm");
		((TextView) convertView.findViewById(R.id.name)).setText(name);
		((TextView) convertView.findViewById(R.id.score)).setText("(" + formatter.format(time) + ") "+ score);

		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return false;
	}
	
}
